import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../config/app_theme.dart';
import '../providers/app_state.dart';
import 'package:flutter/services.dart';
import 'login_screen.dart';
import 'main_screen.dart';

class SplashScreen extends StatefulWidget {
  @override
  _SplashScreenState createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    
    // 设置状态栏颜色为透明
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
    ));

    // 创建动画
    _controller = AnimationController(
      duration: Duration(seconds: 2),
      vsync: this,
    );
    
    _animation = CurvedAnimation(
      parent: _controller,
      curve: Curves.easeInOut,
    );

    // 开始动画
    _controller.forward();

    // 初始化应用状态
    _initializeApp();
  }

  Future<void> _initializeApp() async {
    // 获取AppState
    final appState = Provider.of<AppState>(context, listen: false);
    
    // 等待2秒以显示启动画面
    await Future.delayed(Duration(seconds: 2));
    
    // 初始化应用状态
    await appState.initialize();
    
    // 根据登录状态导航到不同页面
    if (appState.isLoggedIn) {
      Navigator.pushReplacement(
        context, 
        MaterialPageRoute(builder: (context) => MainScreen())
      );
    } else {
      Navigator.pushReplacement(
        context, 
        MaterialPageRoute(builder: (context) => LoginScreen())
      );
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppTheme.primaryColor,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 应用Logo
            ScaleTransition(
              scale: _animation,
              child: Container(
                width: 120,
                height: 120,
                decoration: BoxDecoration(
                  color: Colors.white,
                  shape: BoxShape.circle,
                ),
                child: Icon(
                  Icons.pets,
                  size: 80,
                  color: AppTheme.primaryColor,
                ),
              ),
            ),
            SizedBox(height: 24),
            // 应用名称
            FadeTransition(
              opacity: _animation,
              child: Text(
                '宠物乐园',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 32,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            SizedBox(height: 16),
            // 应用口号
            FadeTransition(
              opacity: _animation,
              child: Text(
                '分享快乐，守护爱宠',
                style: TextStyle(
                  color: Colors.white.withOpacity(0.8),
                  fontSize: 16,
                ),
              ),
            ),
            SizedBox(height: 80),
            // 加载指示器
            CircularProgressIndicator(
              valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
            ),
          ],
        ),
      ),
    );
  }
} 